package com.store.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.store.entity.Cart;
import com.store.vo.CartVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;

/**
 * 购物车Mapper接口
 */
@Mapper
public interface CartMapper extends BaseMapper<Cart> {
    
    /**
     * 查询用户购物车列表（包含商品详情）
     */
    @Select("SELECT c.id, c.user_id, c.product_id, c.quantity, c.create_time, c.update_time, " +
            "p.name as product_name, p.image as product_image, p.price, p.stock, p.status, " +
            "(p.price * c.quantity) as subtotal " +
            "FROM cart c " +
            "LEFT JOIN products p ON c.product_id = p.id " +
            "WHERE c.user_id = #{userId} " +
            "ORDER BY c.create_time DESC")
    List<CartVO> selectCartListByUserId(@Param("userId") Long userId);
    
    /**
     * 查询用户某个商品的购物车记录
     */
    @Select("SELECT * FROM cart WHERE user_id = #{userId} AND product_id = #{productId}")
    Cart selectByUserIdAndProductId(@Param("userId") Long userId, @Param("productId") Long productId);
}


